<?php
/********************************************************************/
/* Programmer: Stephen Chappell                 					*/
/*       Date: 2/11/2013                           					*/
/*  Page Name: register_user.php                           			*/
/*                                                					*/
/********************************************************************/

/********************************************************************/
/* PHP Registration System                               			*/
/*                                                					*/
/********************************************************************/

/********************************************************************/
/*   Date        Reviser       Revision           					*/
/* --------     ---------     ----------          					*/
/* 2/11/13      S. Chappell   Created prelimiary code for system.   */
/*                                                					*/
/********************************************************************/
   
    /* FUNCTIONS */
    require_once('PasswordHash.php');
    require_once('webservices.php');
    require_once('login.php');
    
    $DEFAULT_POWER_ID = 2;
    $DEFAULT_AVATAR = '12.jpeg';
    
    $first_name = $last_name = $gender = $email = $password = '';
    @$first_name = $_REQUEST['first_name'];
    @$last_name = $_REQUEST['last_name'];
    @$gender = $_REQUEST['gender'];
    @$email = strtolower($_REQUEST['email']);
    @$password = $_REQUEST['password'];
	@$hashPassword = create_hash($password);
	
    $database = mysqli_connect($SERVER_ADDRESS, $SERVER_USERNAME, $SERVER_PASSWORD, 'cs414');
   
    $stmt = $database->prepare('INSERT INTO cs_authentication
                                            (email, salt_hash, last_name, first_name, gender, power_id, avatar)
                                     VALUES (?, ?, ?, ?, ?, ?, ?);');
    $stmt->bind_param('sssssis', $email, $hashPassword, $last_name, $first_name, $gender, $DEFAULT_POWER_ID, $DEFAULT_AVATAR);
    
    if (!$stmt->execute())
    {
        header("Location: registration.php?field=emailerror");
        mysqli_close($database);
    }
    else
    {
        mysqli_commit($database);
        mysqli_close($database);
        
        $_REQUEST['UserName'] = $email;
        $_REQUEST['Password'] = $password;
        main();
    }
?>